﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Bling.DSL;
using Bling.WPF;
using Bling.Util;
using System.Windows.Media.Imaging;

namespace Bling.Tutorial {
  public class TransitionsTest : Canvas {
    public static readonly ImageBrush Autumn = new ImageBrush("Resources/Autumn.jpg".ImageFor(typeof(TransitionsTest)));
    public static readonly ImageBrush Desert = new ImageBrush("Resources/Dock.jpg".ImageFor(typeof(TransitionsTest)));

    public TransitionsTest() {
      var Autumn = TransitionsTest.Autumn;
      var Desert = TransitionsTest.Desert;


      CanvasBl MainCanvas = this;
      MainCanvas.Background = Brushes.White;
      var Progress = new LabelSliderBl(MainCanvas) {
        Slider = { Minimum = 0d, Maximum = 1d, },
        RightBottom = MainCanvas.Size, ZIndex = 1, IsEnabled = false,
        LabelName = "Progress", WidthRatio = .15, 
      };
      var RandomSeed = new LabelSliderBl(MainCanvas) {
        Slider = { Minimum = 0d, Maximum = 1d, Value = (new Random()).NextDouble(), },
        RightBottom = Progress.RightTop, ZIndex = 1, IsEnabled = Progress.IsEnabled,
        LabelName = "Random", WidthRatio = .15, 
      };
      var Canvas0 = new CanvasBl(MainCanvas) {
        Size = MainCanvas.Size, LeftTop = new PointBl(0, 0), ZIndex = 0, Background = Autumn,
      };
      var lbl = new LabelBl(Canvas0) {
        CenterPosition = Canvas0.CenterSize, Font = { Weight = FontWeights.SemiBold }, 
        Content = "Center!",
      };
      lbl.Font.Size = lbl.IsMouseOver.Condition(40, 20);

      PointBl ButtonAt = new PointBl(0, 0);
      BoolBl LastChecked = null;
      Action<string, Func<BrushBl, PointBl, ColorBl>> AddShader = (name, shader) => {
        var button = new ToggleButtonBl(MainCanvas) {
          ZIndex = 1, Content = name, LeftTop = ButtonAt,
          Click0 = (self) => {
            if (((object)LastChecked) != null) LastChecked.Now = false;
            else Progress.IsEnabled.Now = true;
            Canvas0.Effect.Custom = shader;
            LastChecked = self.IsChecked;
            LastChecked.Now = true;
          },
        };
        ButtonAt = button.RightTop;
      };
      AddShader("Water", (input, uv) => ShaderLib.Water(Desert, input, uv, Progress, RandomSeed));
      AddShader("Blood", (input, uv) => ShaderLib.Blood(Desert, input, uv, Progress, RandomSeed));
      AddShader("Crumple", (input, uv) => ShaderLib.Crumple(Desert, input, uv, Progress, RandomSeed));
      AddShader("RotateCrumple", (input, uv) => ShaderLib.RotateCrumple(Desert, input, uv, Progress, RandomSeed));
      AddShader("RotateWiggle", (input, uv) => ShaderLib.RotateWiggle(Desert, input, uv, Progress, RandomSeed));
    }
  }
}
